package com.shrikanthravi.customnavigationdrawer2.utils;


import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

public class ResourceUtil {
    private final Context mContext;

    public ResourceUtil(Context context) {
        this.mContext = context;
    }


    /**
     * 设置背景图片
     *
     * @param res 图片资源
     * @return Element
     */
    public Element getImageDrawable(int res) {
        PixelMapElement drawable = null;
        try {
            drawable = new PixelMapElement(
                    mContext.getResourceManager().getResource(res));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return drawable;
    }
    /**
     * 通过颜色id 获取elemet对象
     *
     * @param res res
     * @return Element
     */
    public Element getColorDrawable(int res) {
        int color = getColor(res);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(color));

        return shapeElement;
    }

    /**
     * 获取颜色
     * @param res res
     * @return int
     */
    public int getColor(int res) {
        int result = 0;
        try {
            result = mContext.getResourceManager().getElement(res).getColor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
